package web.mb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale.Category;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.RowEditEvent;

import metier.entities.Langue;
import metier.entities.categorie;
import metier.sess.ICategorieLocal;
import metier.sess.ILangueLocal;


@ManagedBean
@RequestScoped
public class CategorieMB {
	
	@EJB
	private ICategorieLocal metier;
	
	//metier langues
	@EJB
	private ILangueLocal metierLangue;
	private Integer Id;
	private String Intitule;
	private boolean Statut;
	private categorie cat;
	private List<String> listeIntitule=new ArrayList<String>();
	private HashMap<String, String> listeIntituleHashMap=new HashMap<String, String>();
	
	
	public HashMap<String, String> getListeIntituleHashMap() {
		return listeIntituleHashMap;
	}


	public void setListeIntituleHashMap(HashMap<String, String> listeIntituleHashMap) {
		this.listeIntituleHashMap = listeIntituleHashMap;
	}


	public List<String> getListeIntitule() {
		return listeIntitule;
	}


	public void setListeIntitule(List<String> listeIntitule) {
		this.listeIntitule = listeIntitule;
	}




	public String Ajouter()
	{
		
		categorie cat=new categorie("", Statut);
		Integer i=metier.addCategorie(cat);
		cat.setIntitule("Category."+i+".intitule");
		metier.updateCategorie(cat);
		//
		Iterator intitules = listeIntituleHashMap.keySet().iterator();
		while (intitules.hasNext())
		{
		    String clef = (String)intitules.next();
		    String valeur = (String)listeIntituleHashMap.get(clef);
		    Test.append("/TestJava/move/CargaisonWeb/src/web/resources/welcome_"+clef+".properties", cat.getIntitule()+"="+valeur);
		}
		
	//
		return "ManageCategory.xhtml";
		
	}
	
	public String edit(Integer Id)
	{
		this.Id=Id;
		categorie c=metier.getCategorie(Id);
		this.Intitule=c.getIntitule();
		this.Statut=c.getStatus();
		return "EditCategory.xhtml";
	}
	public String Modifier()
	{
		categorie c=metier.getCategorie(Id);
		c.setStatus(Statut);
		metier.updateCategorie(c);
		Iterator intitules = listeIntituleHashMap.keySet().iterator();
		while (intitules.hasNext())
		{
		    String clef = (String)intitules.next();
		    String valeur = (String)listeIntituleHashMap.get(clef);
		    Test.readReplace("/TestJava/move/CargaisonWeb/src/web/resources/welcome_"+clef+".properties",valeur,c.getIntitule());
		}
		return "ManageCategory.xhtml";
	}

	public String Supprimer(Integer Id)
	{
		
		metier.deleteCategorie(Id);
		return "succes";
		
	}
	
	public String Supprimerr()
	{
		
		metier.deleteCategorie(Id);
		return "succes";
		
	}
	
	public List<categorie> getListCategorie()
	{
		return metier.getAllCategories();
	}

	public Integer getId() {
		return Id;
	}

	public void setId(Integer id) {
		Id = id;
	}

	public String getIntitule() {
		return Intitule;
	}

	public void setIntitule(String intitule) {
		Intitule = intitule;
	}

	public boolean isStatut() {
		return Statut;
	}

	public void setStatut(boolean statut) {
		Statut = statut;
	}
	
	public categorie getCat() {
		return cat;
	}

	public void setCat(categorie cat) {
		this.cat = cat;
	}

	public List<Langue> listeLangue()
	{
		return metierLangue.getAllActiveLangues();
	}
	
	//nouveau
	 public void onRowEdit(RowEditEvent event) {
	        FacesMessage msg = new FacesMessage("Car Edited");
	        FacesContext.getCurrentInstance().addMessage(null, msg);
	    }
	 
	 public void onRowCancel(RowEditEvent event) {
	        FacesMessage msg = new FacesMessage("Edit Cancelled");
	        FacesContext.getCurrentInstance().addMessage(null, msg);
	    }
	
}
